/*
 * @ClassName input_check.dart
 * @Description 一些常用的输入框校验
 * @Author SuPeiSen
 * @Date 2020/7/14 14:36
 */

//import 'package:flutter_easyloading/flutter_easyloading.dart';
//
//class InputCheck{
//
//  ///判断字符串是否为空
//  static bool strIsNoNull(String str, {String msg}){
//    if(str == ''){
//      EasyLoading.showError("$msg不能为空");
//      return false;
//    }
//    return true;
//  }
//
//  ///判断字符串长度必须在某个区间
//  static bool strIn(String str, int minLen, int maxLen, {String msg}){
//    bool re = strIsNoNull(str, msg: msg);
//    if(re == false) return false;
//    if(str.length > maxLen || str.length < minLen){
//      EasyLoading.showError("$msg长度必须在$minLen和$maxLen之间");
//      return false;
//    }
//    return true;
//  }
//
//  /// 判断两个变量是否相等
//  static bool doubleEqual(Object one, Object tow, {String msg}){
//    if(one != tow){
//      EasyLoading.showError(msg);
//      return false;
//    }
//    return true;
//  }
//}

/*
  使用策划模式对参数进行校验
 */

import 'package:flutter/cupertino.dart';

class Strategies {
  // 是否为必填
  String require(dynamic value, String message) {
    return (value == null || value == '' || value == 0) ? message : '';
  }

  // 正则匹配
  String pattern(String value, RegExp reg, String message) {
    return reg.hasMatch(value) ? '' : message;
  }

  // 两个是否相等
  String equal(dynamic firstData, dynamic secondData, String message) {
//    assert((firstData is String || firstData is int || firstData is double) == false,
//    {"firstData must be String or int or double"});
//
//    assert((secondData is String || secondData is int || secondData is double) == false,
//    {"secondData must be String or int or double"});

    return firstData == secondData ? '' : message;
  }

  // 最小长度
  String minLength(dynamic value, int len, String message) {
//    assert((value is String || value is int || value is double) == false,
//        {"value must be String or int or double"});

    if (value is String) {
      return value.length >= len ? '' : message;
    }
    var _number = value.toDouble();
    return _number >= len ? '' : message;
  }

  // 最小长度
  String maxLength(dynamic value, int len, String message) {
//    assert((value is String || value is int || value is double) == false,
//        {"value must be String or int or double"});

    if (value is String) {
      return value.length <= len ? '' : message;
    }
    var _number = value.toDouble();
    return _number <= len ? '' : message;
  }
}

// 常用的正则表达式
class Pattern {

  // 邮箱正则表达式
  static const String EMAIL_PATTERN =  r'^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$';
  static final RegExp emailPattern = RegExp(EMAIL_PATTERN);
}

// 定义策划类
class ValidateInput extends Strategies{
  static const String REQUIRE = "require"; // 字段必须
  static const String PATTERN = "pattern"; // 正则表达式
  static const String MIN_LENGTH = "minLength"; // 最小长度
  static const String MAX_LENGTH = "maxLength"; // 最大长度
  static const String EQUAL = "equal"; // 是否相等

  static const String MESSAGE = "message"; // 返回信息

  final Map<String, List<Map<String, dynamic>>> rules; // 每个字段的校验规则
  final Map<String, dynamic> params; // 表示要校验的字段和对应的值

  List<Function> _validateList = [];

  String _message = "";

  String get message => this._message;

  ValidateInput({@required this.rules, @required this.params}){
    _check();
  }

  void _check() {
    Map<String, Function> strategies = {
      'require': require,
      'pattern': pattern,
      'minLength': minLength,
      'maxLength': maxLength,
      'equal': equal,
    };

    // keyIndex: 校验的字段  ruleValue: 校验的规则
    rules.forEach((String keyIndex, dynamic ruleValue) {
      var _value = params[keyIndex]; // 获取校验的值
      var _iterator = rules[keyIndex]; // 值需要校验的规则

      _iterator.forEach((item) {
        // 每个字段需要校验的规则
        item.forEach((key, value) {
          if(key != "message") {
            if (key == "require" && value == true) {
              _validateList.add(() {
                return strategies[key](_value, item['message']);
              });
            } else {
              _validateList.add(() {
                return strategies[key](_value, item[key], item['message']);
              });
            }
          }
        });
      });
    });

    // 把校验不通过的值复制给message
    for (Function func in _validateList) {
      var re = func();
      if (re != '') {
        this._message = re;
        break;
      }
    }

  }
}

